﻿using Basic.FormCollect.Model;
using Basic.FormModel;
using Basic.FormModel.QueryColumn;
using Basic.UserFormService.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.SqlSugar.VirtualQuery;

namespace Basic.UserFormService.PageControl
{
    internal class PageControlService
    {
        protected PhysicsFormBody _Body
        {
            get;
            private set;
        }

        protected readonly Dictionary<long, PhysicsQueryTable> _table = new Dictionary<long, PhysicsQueryTable>();

        protected Dictionary<long, PhysicsQueryColumn> _column;

        protected Dictionary<long, ColTable> _Cols;

        private bool _IsMain = false;

        protected QueryStruct _Query;


        protected void _InitTable ( PhysicsFormBody body, long[] tableId )
        {
            this._Body = body;
            this._Query = new QueryStruct();
            if ( !this._Body.Children.IsNull() )
            {
                if ( tableId.Length == 1 )
                {
                    long id = tableId[0];
                    PhysicsQueryTable table = this._Body.Main.Id == id ? this._Body.Main : this._Body.Children.Find(c => c.Id == id);
                    this._IsMain = table.IsMainTable;
                    this._Query.Table = new TableStruct
                    {
                        Name = table.TableName
                    };
                }
                else
                {
                    this._Query.Table = new TableStruct
                    {
                        Name = this._Body.Main.TableName,
                        Foreign = this._Body.Children.Convert(c => tableId.Contains(c.Id), c => new TableStruct
                        {
                            Filter = new Dictionary<string, string>
                            {
                                {ConstDic.ForeignKeyColName,ConstDic.KeyColName }
                            },
                            JoinType = SqlSugar.JoinType.Left,
                            Name = c.TableName
                        })
                    };
                }
            }
            else
            {
                this._IsMain = true;
                this._Query.Table = new TableStruct
                {
                    Name = this._Body.Main.TableName
                };
            }
            this._Init();
        }
        private void _Init ()
        {
            this._table.Add(this._Body.Main.Id, this._Body.Main);
            this._column = new Dictionary<long, PhysicsQueryColumn>(this._Body.Main.Column);
            if ( !this._Body.Children.IsNull() )
            {
                this._Body.Children.ForEach(c =>
                {
                    this._table.Add(c.Id, c);
                    c.Column.ForEach(( id, col ) =>
                    {
                        this._column.Add(id, col);
                    });
                });
            }
            this._Cols = this._column.ToDictionary(a => a.Key, a => new ColTable
            {
                Name = a.Value.ColName,
                Table = this._GetTable(a.Value),
                DataType = a.Value.DataType,
            });
        }
        private WhereTable _GetTable ( PhysicsQueryColumn col )
        {
            PhysicsQueryTable table = this._table[col.TableId];
            WhereTable t = new WhereTable
            {
                Table = table.TableName
            };
            if ( table.IsMainTable && this._IsMain == false )
            {
                t.Key = ConstDic.KeyColName;
                t.Foreign = ConstDic.ForeignKeyColName;
            }
            else
            {
                t.Key = ConstDic.ForeignKeyColName;
                t.Foreign = ConstDic.KeyColName;
            }
            return t;
        }
    }
}
